use axum::{routing::{get, post}, Router};
use serde::Deserialize;

use crate::{
    error::AppErr, server::model::{cbor_ok, Cbor, CborRet, Verify, Void}, store::proj::{self, TbProj}
};

#[derive(Debug, Deserialize)]
struct CreateReq {
    name: String,
    description: String,
    version: u32,
}

async fn on_create(_v: Verify, req: Cbor<CreateReq>) -> CborRet<i64> {
    let id = proj::create(&req.name, &req.description, req.version).await?;
    cbor_ok(id)
}

async fn on_query() -> CborRet<Box<[TbProj]>> {
    let items = proj::query().await?;
    cbor_ok(items)
}

async fn on_delete(_v: Verify, id: Cbor<i64>) -> Result<Void, AppErr> {
    proj::delete(id.0).await?;
    Ok(Void)
}

pub fn register() -> Router {
    Router::new().route("/create", post(on_create))
        .route("/query", get(on_query))
        .route("/delete", post(on_delete))
}
